package com.msmall.community.bbs.action.wml;

import com.msmall.community.bbs.model.Thread;
import com.msmall.community.bbs.service.ThreadManager;
import com.msmall.community.bbs.util.BbsUtil;
import com.msmall.community.common.util.FilterCharsUtil;
public class ThreadAction extends BBSBaseAction{

	private static final long serialVersionUID = -2972708275132158818L;

	private int result;
	
	private Thread thread;

	private ThreadManager threadManager;
	
	public String save(){
		int resultx = validateSave();
		String toUrl = "viewTopic.do?firstClick=N&topicId=" + topicId;
		if(resultx == 7){
			thread.setNickname(getNickname());
			threadManager.saveThread(thread, boardId, topicId, getUserid(), userAgentType);
		}else{
			toUrl = toUrl + "&result=" + resultx;
			setResult(resultx);
		}

		this.setBbsToUrl(toUrl);
		return SUCCESS;
	}
	
	public int validateSave(){
		String toUrl = "viewTopic.do?firstClick=N&topicId=" + topicId;
		this.setBbsToUrl(toUrl);
		if(null == thread){
		//	this.addFieldError("thread.content", getText("wml.bbs.thread.create.error8"));
		//	addActionError(getText("wml.bbs.thread.create.error8"));
			return 8;
		}
		String content = thread.getContent();
		int contentOK = BbsUtil.checkThreadContentLength(content);
		if(contentOK == 1){
		//	this.addFieldError("thread.content", getText("wml.bbs.thread.create.error4"));
		//	addActionError(getText("wml.bbs.thread.create.error4"));
			return 4;
		}
		if(contentOK == 2){
		//	this.addFieldError("thread.content", getText("wml.bbs.thread.create.error5"));
		//	addActionError(getText("wml.bbs.thread.create.error5"));
			return 5;
		}
		boolean contentC = FilterCharsUtil.containsFilterChars(content);
		if(contentC){
		//	addActionError(getText("wml.bbs.thread.create.error6"));
		//	this.addFieldError("thread.content", getText("wml.bbs.thread.create.error6"));
			return 6;
		}
		
		return 7;
	}
	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public Thread getThread() {
		return thread;
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public void setThreadManager(ThreadManager threadManager) {
		this.threadManager = threadManager;
	}

}
